package com.cnpc.rightslogan.server;

import com.cnpc.rightslogan.entity.User;
import com.cnpc.rightslogan.server.impl.MyUserPrincipal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

//import org.springframework.security.core.userdetails.User;

/**
 * Create by shaojie on 2019/8/7
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private IUserLoginService userLoginServiceImpl;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User paramUser = new User();
        paramUser.setUsername(username);
        User user = userLoginServiceImpl.getUser(paramUser);
        if (user == null) {
            throw new UsernameNotFoundException(username);
        }
        return new MyUserPrincipal(user);
    }

    //    这里的 User 对象是框架提供的一个用户对象，注意包名是：org.springframework.security.core.userdetails.User，
//    里面的属性中最核心的就是password，username和authorities。
//    @Override
//    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//        Collection<GrantedAuthority> authorities = new ArrayList<>();
//        authorities.add(new SimpleGrantedAuthority("ADMIN"));
//        return new User("root", new BCryptPasswordEncoder().encode("root"), authorities);
//    }
}